package com.controller;

import com.pojo.Role;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    public String editPage(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null){
            return "login";
        }
        user = userService.selectById(user.getId());
        model.addAttribute("user", user);
        return "user/info";
    }

    @PostMapping("/update")
    public String update(User user, RedirectAttributes redirectAttributes) {
        userService.update(user);
        redirectAttributes.addFlashAttribute("successMessage", "基本信息更新成功！");
        return "redirect:/user/info";
    }
}
